<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "activitys".
 *
 * @property integer $id
 * @property string $stu_id
 * @property integer $year
 * @property integer $semester
 * @property string $date
 * @property string $time
 * @property string $activity
 * @property string $type
 */
class ActivityModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'activitys';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id'], 'required'],
            [['semester'], 'integer'],
            [['date'], 'safe'],
            [['stu_id', 'type','name','year'], 'string', 'max' => 20],
            [['time'], 'string', 'max' => 50],
            [['activity'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '编码ID',
            'stu_id' => '学号',
            'year' => '学年',
            'semester' => '学期',
            'date' => '日期',
            'time' => '时间',
            'activity' => '活动名称',
            'type' => '类型',
        		'name'=>'姓名',
        ];
    }


    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{


            foreach ($data as $value){
                $person=new ActivityModel();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->name=$value['姓名'].'';
                $person->year=$value['学年'].'';
                $person-> semester=(int)$value['学期'];
                $person->date=$value['日期'].'';
                $person->time=$value['时间'].'';
                $person->activity=$value['活动名称'].'';
                $person->type=$value['类型'].'';

                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }
}
